﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LiveCharts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Monitor
{

    internal partial class TimeLineData : ObservableObject
    {
        System.Timers.Timer timer;


        [ObservableProperty]
        private List<MomentData> _momentDatas = new List<MomentData>();

        [ObservableProperty]
        private int _nowValue = 0;

        public ChartValues<string> TimeLineKey => GetKey();
        public ChartValues<int> TimeLineValue => GetValue();


        private int MaxCount = 300;

        public TimeLineData()
        {
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }

        [RelayCommand]
        public void SetValue(int value)
        {
            NowValue = value;
            AddMomentData();
        }

        private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            AddMomentData();
        }

        
        private void AddMomentData()
        {
            MomentData newData = new MomentData() { TheMoment = DateTime.Now, DataValue = NowValue };

            MomentDatas.Add(newData);

            while (MomentDatas.Count > MaxCount)
            {
                MomentDatas.RemoveAt(0);
            }

            OnPropertyChanged(nameof(TimeLineKey));
            OnPropertyChanged(nameof(TimeLineValue));
        }

        ChartValues<string> resultKeyS = new ChartValues<string>();
        private ChartValues<string> GetKey()
        {
            if (resultKeyS.Count==0)
            {
                for (int i = 0; i < MomentDatas.Count; i++)
                {
                    resultKeyS.Add(MomentDatas[i].TheMoment.ToString("mm:ss fff"));
                }
            }
            else
            {
                resultKeyS.Add(MomentDatas[MomentDatas.Count-1].TheMoment.ToString("mm:ss fff"));
            }
            

            while (resultKeyS.Count > MaxCount)
            {
                resultKeyS.RemoveAt(0);
            }
            return resultKeyS;
        }

        ChartValues<int> resultValueS = new ChartValues<int>();
        private ChartValues<int> GetValue()
        {
            if (resultValueS.Count==0)
            {
                for (int i = 0; i < MomentDatas.Count; i++)
                {
                    resultValueS.Add(MomentDatas[i].DataValue);
                }
            }
            else
            {
                resultValueS.Add(MomentDatas[MomentDatas.Count-1].DataValue);
            }
            
            if (resultValueS.Count > MaxCount)
            {
                resultValueS.RemoveAt(0);
            }
            return resultValueS;
        }
    }

    internal partial class MomentData : ObservableObject
    {
        [ObservableProperty]
        private DateTime _theMoment;
        [ObservableProperty]
        private int _dataValue;

        public MomentData() { }
    }
}
